package com.itheima.controller;


import com.itheima.pojo.Category;
import com.itheima.pojo.Result;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController// 标记为控制器类,并且返回json数据
@RequestMapping("/category")// 请求路径
public class CategoryController {

    @Autowired// 注入service
    private CategoryService categoryService;

    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
        categoryService.add(category);
        return Result.success();
    }


    // 查询分类列表
    @GetMapping
    public Result<List<Category>> List() {
        List<Category> cs = categoryService.list();
        return Result.success(cs);
    }

    // 根据id查询分类信息
    @GetMapping("/detail")
    public Result<Category> detail(Integer id){

        Category c = categoryService.findById(id);
        return Result.success(c);
    }

    // 更新分类
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }

    // 删除分类
    @DeleteMapping
    public Result delete(@Validated(Category.Delete.class) Integer id) {
        // 删除分类
        categoryService.delete(id);

        return Result.success();
    }

}
